//
//  UIView+HHExtension.m
//  百思不得姐
//
//  Created by LOVE on 16/3/9.
//  Copyright © 2016年 LOVE. All rights reserved.
//

#import "UIView+HHExtension.h"

@implementation UIView (HHExtension)
-(void)setSize:(CGSize)size{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;

}
-(CGSize)size{
    return self.frame.size;

}
-(void)setWidth:(CGFloat)width{
    CGRect frame = self.frame;
    frame.size.width  =width;
    self.frame = frame;

}
-(void)setHeight:(CGFloat)height{

    CGRect  frame = self.frame;
    frame.size.height = height;
    self.frame =frame;
    

}
-(void)setX:(CGFloat)x{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
    
}

-(void)setY:(CGFloat)y{

    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}
-(void)setCenterX:(CGFloat)centerX{

    CGPoint center = self.center;
    center.x = centerX;
    
    self.center  =center;
    

}
-(void)setCenterY:(CGFloat)centerY{
    
    CGPoint center = self.center;
    center.y = centerY;
    
    self.center  =center;
    
    
}
-(CGFloat)centerX{
    
    return self.center.x;
    
}
-(CGFloat)centerY{
    
    return self.center.y;
    
}

-(CGFloat)width{

    return self.frame.size.width;

}

-(CGFloat)height{

    return self.frame.size.height;
}
-(CGFloat)x{

    return self.frame.origin.x;
    
}
-(CGFloat)y{

    return self.frame.origin.y;
}



-(CGFloat)right{

    return CGRectGetMaxX(self.frame);

}

-(void)setRight:(CGFloat)right{

    self.x = right-self.width;

}

-(CGFloat)bottom{

    return CGRectGetMaxY(self.frame);
}
-(void)setBottom:(CGFloat)bottom{
    self.y = bottom- self.height;
    

}
/**判断一个空间是否是真正显示在主窗口 */
-(BOOL)isShowingOnKeyWindow{
    
    UIWindow *keyWindow =[UIApplication sharedApplication].keyWindow;
    
    CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
    
    CGRect winBounds = keyWindow.bounds;
    
    BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
    
    return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;


}
+(instancetype)viewFromXib{
   return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];

}

//输出所有子控件
+ (UIView *)hh_foundViewInView:(UIView *)view clazzName:(NSString *)clazzName{

    // 递归出口
    if ([view isKindOfClass:NSClassFromString(clazzName)]) {
        return view;
    }
    // 遍历所有子视图
    for (UIView *subView in view.subviews) {
        UIView *foundView = [self hh_foundViewInView:subView clazzName:clazzName];
        if (foundView) {
            return foundView;
        }
    }
    return nil;
}

@end
